package cn.edu.scau.cmi.ema.service;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Echart;
import cn.edu.scau.cmi.ema.service.base.CmiEmaService;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.*;
@Service("EchartService")
@Transactional

public class EchartServiceImpl extends CmiEmaService implements EchartService {
	public EchartServiceImpl() {
	}

	@Transactional
	public Echart saveEchart(Echart echart) {

		if (echart.getId() == null) {

			Echart existingEchart = echartDAO.findEchartByPhysicalPrimaryKey(echart.getName());

			if (isEquals(echart, existingEchart)) {
				return existingEchart;
			}

			echart = echartDAO.store(echart);
			echartDAO.flush();

		} else {
			Echart existingEchart = echartDAO.findEchartByPrimaryKey(echart.getId());

			echart = echartDAO.store(echart);
			echartDAO.flush();
		}

		return echart;
	}

	public void deleteEchart(Echart echart) {
		echartDAO.remove(echart);
		echartDAO.flush();
	}

	@Transactional
	public void deleteEcharts(Set<Integer> deleteEchartsId) {
		Query query = echartDAO.createNamedQuery("deleteEchartsById", -1, -1);
		query.setParameter(1, deleteEchartsId);
		query.executeUpdate();
	}

	@Transactional
	public Set<Echart> loadEcharts() {
		return echartDAO.findAllEcharts();

	}

	@Transactional
	public Set<Echart> loadReferenceEcharts(Set<Echart> echarts) {
		Iterator<Echart> echartsIterator = echarts.iterator();
		Set<Echart> referencedEcharts = new HashSet<>();
		while (echartsIterator.hasNext()) {
			Echart currentEchart = echartsIterator.next();

		}
		return referencedEcharts;
	}

	public Set<Echart> loadEchartsByIdSet(Set<Integer> echartIdSet) {

		Iterator<Integer> echartIdIterator = echartIdSet.iterator();
		Set<Echart> echartSet = new HashSet<>();
		while (echartIdIterator.hasNext()) {
			Integer id = echartIdIterator.next();
			Echart currentEchart = echartDAO.findEchartById(id);
			echartSet.add(currentEchart);
		}
		return echartSet;
	}

	@Transactional
	public List<Echart> findAllEcharts(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Echart>(echartDAO.findAllEcharts(startResult, maxRows));
	}

	@Transactional
	public Echart findEchartByPrimaryKey(Integer id) {
		return echartDAO.findEchartByPrimaryKey(id);
	}

	@Transactional
	public Integer countEcharts() {
		return ((Long) echartDAO.createQuerySingleResult("select count(o) from Echart o").getSingleResult()).intValue();
	}

	@Transactional
	public Integer countEcharts(CmiPagination page) {
		String sql = CmiSqlUtil.createCountHql(page, Echart.class);
		return ((Long) echartDAO.createQuerySingleResult(sql).getSingleResult()).intValue();
	}

	/**
	 * search for name with ajax
	 *
	 */
	@Transactional
	public Integer countEchartsByName(String queryName) {
		Query query = echartDAO.createNamedQuery("countEchartsByName", -1, -1);
		query.setParameter(1, queryName);
		return new Long((long) query.getSingleResult()).intValue();
	}

	@Transactional
	public Integer countEchartsByDetachedCriteria(CmiPagination page) {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Echart.class);
		Session session = (Session) entityManager.getDelegate();
		detachedCriteria.setProjection(Projections.rowCount());
		return new Long((long) detachedCriteria.getExecutableCriteria(session).uniqueResult()).intValue();
	}

	public Map<String, ?> loadEchartObjectReferencedSet(Echart echart) {
		HashMap<String, Object> objectReferencedSetMap = new HashMap<>();

		return objectReferencedSetMap;
	}

	public Map<String, ?> loadEchartClassReferencedSet(Echart echart) {
		HashMap<String, Object> classReferencedSetMap = new HashMap<>();

		return classReferencedSetMap;
	}

	public boolean isEquals(Echart echart, Object obj) {
		if (obj == echart)
			return true;
		if (!(obj instanceof Echart))
			return false;
		Echart comparedEchart = (Echart) obj;
		if (!CheckPropertyEqual(echart.getName(), comparedEchart.getName())) {
			return false;
		}
		return true;
	}

	public static <T> boolean CheckPropertyEqual(T originProperty, T comparedProperty) {
		// 基本类型经过参数传递后会自动装箱
		String[] className = originProperty.getClass().getName().split("\\.");
		if (className[0].equals("java") && className[1].equals("math")) {
			return ((Comparable) originProperty).compareTo(comparedProperty) == 0 ? true : false;
		} else {
			return originProperty.equals(comparedProperty);
		}
	}
}

